/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.composite.editpart;

import java.beans.PropertyChangeListener;
import java.util.Iterator;

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.uml2.uml.CollaborationUse;
import org.eclipse.uml2.uml.Dependency;
import org.eclipse.uml2.uml.Property;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.LinkNameDisplayEditPart;
import com.cea.papyrus.core.editpart.connection.NamedGraphEdgeEditPart;
import com.cea.papyrus.core.figures.connection.DashEdgeFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Controller of the Role binding.
 */
public class RoleBindingEditPart extends NamedGraphEdgeEditPart implements PropertyChangeListener {

	/**
	 * Creates a new RoleBindingEditPart
	* @param graphEdge the graph edge representing the roel binding
	 * @param modelManager the model manager associated to the editor
	 */
	public RoleBindingEditPart(GraphEdge graphEdge, ModelManager modelManager) {
		super(graphEdge,modelManager);
	}

    /**
     * Install diagram specific editPolicy
     */
    @Override
    protected void createEditPolicies(){
        super.createEditPolicies();
        // overload the GRAPHICAL_NODE_ROLE editPolicy to use the diagram specific one
        installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new GraphNodeEditPolicy() );
    }

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.edge.StereotypedGraphEdgeEditPart#createFigure()
	 */
	@Override
	protected IFigure createFigure() {
		DashEdgeFigure f= new DashEdgeFigure(false);
		f.setArrow(false);
		return f;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.edge.NamedGraphEdgeEditPart#createChild(java.lang.Object)
	 */
	@Override
	protected EditPart createChild(Object model) {
		if( model instanceof GraphNode) {
			// index in contained edge = 0
			GraphNode element = (GraphNode)getModelChildren().get(0);	// getModelChildren only returns GraphNodes
			if((((GraphNode)model) == ((GraphNode)element)) ) {
	
				//look for the property of the Collaboration
				// look for the property tha plays a role
				Property player=null;				
				if(((Uml1SemanticModelBridge)getGraphEdge().getAnchorSource().getSemanticModel()).getElement() instanceof CollaborationUse){
					player= (Property)((Uml1SemanticModelBridge)getGraphEdge().getAnchorTarget().getSemanticModel()).getElement();	
				}
				else{
					player= (Property)((Uml1SemanticModelBridge)getGraphEdge().getAnchorSource().getSemanticModel()).getElement();	
				}
				//this the role of the binding
				Property role=null;
				//look  for dependencies
				Iterator itbinding=getUmlElement().getRoleBindings().iterator();
				
				//look for the dependency where the source is the player
				while(itbinding.hasNext()){
					Dependency dependency= (Dependency)itbinding.next();
					Iterator itSource=dependency.getClients().iterator();
					while(itSource.hasNext()){
						Property source= (Property)itSource.next();
						if (source.equals(player)){
							// we have found the good dependency
							role=(Property)dependency.getSuppliers().get(0);
						}
					}
				}
				
				theNameLabel =new LinkNameDisplayEditPart(
						(GraphNode)element,
						role,
						modelManager);
				return theNameLabel;
			}
		}
		return super.createChild(model);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphEdgeEditPart#getUmlElement()
	 */
	@Override
	public CollaborationUse getUmlElement() {
		return (CollaborationUse)((Uml1SemanticModelBridge)getGraphEdge().getSemanticModel()).getElement();
	}
	
}